const state = {
  sidebar: {
    opened: sessionStorage.getItem("sidebarStatus")
      ? !!+sessionStorage.getItem("sidebarStatus")
      : true,
    withoutAnimation: false,
  },
  visitedRoutes: [
    {
      name: "dashboard",
      path: "/",
      title: "主页",
    },
  ],
};

const mutations = {
  SET_VISITEDROUTES: (state, visitedRoutes) => {
    state.visitedRoutes = visitedRoutes;
  },
  TOGGLE_SIDEBAR: (state) => {
    state.sidebar.opened = !state.sidebar.opened;
    state.sidebar.withoutAnimation = false;
    if (state.sidebar.opened) {
      sessionStorage.setItem("sidebarStatus", 1);
    } else {
      sessionStorage.setItem("sidebarStatus", 0);
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    sessionStorage.setItem("sidebarStatus", 0);
    state.sidebar.opened = false;
    state.sidebar.withoutAnimation = withoutAnimation;
  },
};

const actions = {
  updateRoutes({ commit }, routes) {
    commit("SET_VISITEDROUTES", routes);
  },
  toggleSideBar({ commit }) {
    commit("TOGGLE_SIDEBAR");
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit("CLOSE_SIDEBAR", withoutAnimation);
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
